<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    class="ant-modal2"
    @cancel="handleCancel"
  >
    <a-form :form="form">

      <a-form-item v-show="false" >
        <a-input v-decorator="['id']" type="hidden"/>
      </a-form-item>
      <a-divider orientation="left">报修详情</a-divider>
      <row-list :col="2">
        <row-item>
          <a-form-item
            label="设备名称"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ model.sbName }}</label>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="部件名称"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ model.partName }}</label>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="报修人"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ model.repairUserName }}</label>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="报修单号"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ model.applicationNo }}</label>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            class="marginBottom"
            label="报修时间"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ model.applicationApplyTime }}</label>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="报修来源"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ BaseTool.Object.getField(sourceMap,model.applicationSource) }}</label>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="紧急等级"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ BaseTool.Object.getField(levelMap,model.applicationLevel) }}</label>
          </a-form-item>
        </row-item>
      </row-list>
      <a-divider orientation="left">维修详情</a-divider>
      <row-list :col="2">
        <row-item>
          <a-form-item
            label="维修单号"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ model.repairNo }}</label>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="维修开始时间"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ model.startTime }}</label>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="维修结束时间"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ model.endTime }}</label>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="维修人员"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ model.repairPersonName }}</label>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            class="marginBottom"
            label="维修描述"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ model.repairDescripition }}</label>
          </a-form-item>
        </row-item>
      </row-list>
      <a-divider orientation="left">审核</a-divider>
      <row-list :col="1">
        <row-item>
          <a-form-item
            label="驳回原因"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-textarea
              :rows="4"
              v-decorator="['descripition', {rules: [{required: true, message: '审核描述不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="备注"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-textarea
              :rows="4"
              v-decorator="['remark']"/>
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
    <repair-application-form-select-modal ref="rafSelectModal" @selected="handleAppNoSelected"/>
    <repair-form-select-modal ref="repairSelectModal" @selected="handleRepairNoSelected"/>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addRepairCheck, returnRepair, updateRepairCheck } from '@/api/repair/check'
import RepairApplicationFormSelectModal from '@/views/repair/application-form/modules/RepairApplicationFormSelectModal'
import RepairFormSelectModal from '@/views/repair/form/modules/RepairFormSelectModal'
export default {
  name: 'BaseRepairCheck',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      model: {},
      // 下拉框map
      statusMap: {},
      sourceMap: {},
      levelMap: {}
    }
  },
  components: {
    RepairApplicationFormSelectModal,
    RepairFormSelectModal
  },
  props: {
  },
  created () {
    // 下拉框map
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPAIR_CHECK_STATUS)
    this.sourceMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPAIR_APPLICATION_FORM_SOURCE)
    this.levelMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPAIR_APPLICATION_FORM_LEVEL)
  },
  methods: {
    base (record) {
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        return
      }
      this.model = record
      this.modalTitle = '驳回'
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'descripition',
          'remark'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        returnRepair(values).then(() => {
          this.handleCancel(values)
        }).catch(() => {
          this.confirmLoading = false
        })
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    },
    handleAppNoSelect () {
      this.$refs.rafSelectModal.base()
    },
    handleAppNoSelected (record, keys, rows) {
      console.log(keys, rows)
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'applicationId': key,
          'applicationNo': row.no
        }))
      })
    },
    handleRepairNoSelect () {
      this.$refs.repairSelectModal.base()
    },
    handleRepairNoSelected (record, keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'repairId': key,
          'repairNo': row.no
        }))
      })
    }

  }
}
</script>
